/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.insurance.cis.daos;

import com.insurance.cis.connection.CisConnection;
import com.insurance.cis.models.Module;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author TruongLQ
 */
public class ModuleDao {
    
    public List<Module> getModules(int groupId) {
        try {
            final String SQL = "select * from Module where ModuleID in "
                    + "(select ModuleID from Permission where GroupID=?)";
            PreparedStatement pst = CisConnection.getInstance().getSqlConnection()
                    .prepareStatement(SQL);
            pst.setInt(1, groupId); 
            ResultSet rs = pst.executeQuery();
            List<Module> moduleList = new ArrayList<>();
            while (rs.next()) {
                Module module = new Module(rs.getInt(1), rs.getString(2));
                moduleList.add(module);
            }
            return moduleList;
        } catch (SQLException ex) {
            Logger.getLogger(ModuleDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return Collections.emptyList();
    }
}
